package com.poc.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.cloud.task.listener.annotation.AfterTask;
import org.springframework.cloud.task.listener.annotation.BeforeTask;
import org.springframework.cloud.task.listener.annotation.FailedTask;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@EnableBinding(Source.class)
public class SendMessageRunner implements CommandLineRunner {
    private static final Logger LOGGER = LoggerFactory.getLogger(SendMessageRunner.class);
    @Autowired
    private Source source;

    @BeforeTask
    public void beforeTask(TaskExecution taskExecution) {
        LOGGER.info("task start..{}", taskExecution.toString());
    }


    @AfterTask
    public void afterTask(TaskExecution taskExecution) {
        LOGGER.info("task end..{}", taskExecution.toString());
    }


    @FailedTask
    public void failedTask(TaskExecution taskExecution) {
        LOGGER.info("task fail..{}", taskExecution.toString());
    }


    public void run(String... args) throws Exception {
        LOGGER.info("input argument:{}", Arrays.toString(args));
        source.output().send(new GenericMessage<>(Arrays.toString(args)));
    }
}
